package san.dao.dictionary;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import san.dao.HibernateDao;
import san.entity.dictionary.Dictionary;

@Repository
public class DictionaryDao extends HibernateDao<Dictionary, String> {
	/**
	 * 得到字典表的根对象.
	 */
	public Dictionary getDictionaryRoot() throws Exception {
		String hql="from Dictionary t where t.insertDate=(select min(insertDate) from Dictionary)";
		Dictionary root=findUnique(hql);
		return root;
	}

	/**
	 * 插入字典表的根对象.
	 */
	public void insertDictionaryRoot(Dictionary dictionary) throws Exception {
		if (StringUtils.isBlank(dictionary.getParentId())) {
			dictionary.setParentId("sangoisgood");
			save(dictionary);
		}
	}

	/**
	 * 插入字典表对象.
	 */
	public void insertDictionary(Dictionary dictionary) throws Exception {
		if (StringUtils.isNotBlank(dictionary.getParentId())) {
			save(dictionary);
		} else {
			insertDictionaryRoot(dictionary);
		}
	}
	/**
	 * 按id获取对象.
	 */
	@Override
	public Dictionary get(String id) {
		Assert.notNull(id, "id不能为空");
		return (Dictionary) getSession().get(entityClass, id);
	}

}
